<?php

namespace App\Libs;


use Firebase\JWT\JWT;

class Encryption
{
    const SECONDS = 3600 * 24;
    const ALG = 'HS256';

    /**
     * 获取加密key
     * @return mixed|string
     */
    public static function getKey()
    {
        $key = env('JWT_KEY');
        return $key ? $key : 'MqInozNmqLIEMytNVGoROZn7w5wg85vQKmq';
    }

    /**
     * 进行加密
     * @param $uid
     * @return string
     */
    public static function setLoginToken($uid)
    {
        $key = self::getKey();
        $token = [
            'sub' => $uid,
            'expire' => self::SECONDS,//一天过期
            'currentTime' => time(),
        ];
        return JWT::encode($token, $key);


    }

    /**
     * 进行解密
     * @param $token
     * @return array
     */
    public static function getLoginToken($token)
    {
        try {
            $decoded = (array)JWT::decode($token, self::getKey(), [self::ALG]);
            return $decoded;
        } catch (\Exception $e) {
            return [];
        }
    }

    /**
     * 进行手机号码加密
     * @param $uid
     * @return string
     */
    public static function setPhoneToken($uid)
    {
        $key = self::getKey();
        return JWT::encode($uid, $key);
    }
    /**
     * 加密数组
     * @param $input
     * @return string
     */
    public static function setArray($input)
    {
        $key = self::getKey();
        return JWT::encode($input, $key);
    }
}